﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MobileShop
{
    public class Utilities
    {
        public static string GetImageUrl(string relUrl)
        {
            if (string.IsNullOrEmpty(relUrl))
                return "";
            return POSSystem.BLL.Utilities.GetConfigAppSetting("ProductImagePath") + "/" + relUrl.ToString().Replace("~/", "");
        }

        public static string GetValidParam(string parm)
        {
            return System.Text.RegularExpressions.Regex.Replace(parm.Trim(new char[]{' ', '_'}), "[ -?/\\!@#$%^&*~`]", "_").Replace("____", "_").Replace("___", "_").Replace("__", "_");
        }

        public static int GetParmValue(string parm)
        {
            string[] temps = parm.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
            return int.Parse(temps[temps.Length - 1]);
        }

        public static string GetKeySetting(string key)
        {
            return System.Configuration.ConfigurationSettings.AppSettings[key];
        }

        public static string Root
        {
            get
            {
                string sPath = HttpContext.Current.Request.ApplicationPath;
                if (sPath.Equals("/"))
                {
                    sPath = "";
                }
                return sPath;
            }
        }

        public static string GetSubString(string input, int length)
        {
            try
            {
                return input.Substring(0, input.Substring(0, length).LastIndexOf(" ")) + " ...";
            }
            catch (Exception ex)
            {
                return input;
            }
        }
    }
}